/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * User Password Class
 *		Provides a secure, in-memory password storage mechanism.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Adaptive.Foundation;

namespace Adaptive.Foundation.Security
{
    /// <summary>
    /// Provides a secure, in-memory password storage mechanism.
    /// </summary>
    public sealed class UserPassword : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Byte array content.
        /// </summary>
        private ProtectedByteArray _content;
        #endregion

        /*--------------------------------------------------------------------------------
		 * Constructor / Dispose / Destructor Methods
		 *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public UserPassword()
        {
            _content = new ProtectedByteArray();
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
                if (_content != null)
                    _content.Dispose();
            }

            _content = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
		 * Public Properties
		 *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the value being stored in memory.
        /// </summary>
        /// <value>
        /// A password alue.
        /// </value>
        public string Value
        {
            get
            {
                string returnValue = string.Empty;		//Return value.

                if ((_content.Value != null) && (_content.Value.Length > 0))
                {
                    returnValue = Conversion.ToString(_content.Value).Trim();
                }
                return returnValue;
            }
            set
            {
                string data = null;			//data to store.

                if ((value == null) || (value.Length == 0))
                    _content.Value = null;
                else
                {
                    data = value;
                    while ((data.Length > 0) && (data.Length % 16 != 0))
                        data += " ";
                    _content.Value = Conversion.ToBytes(data);
                }
            }
        }
        #endregion
    }
}
